// 房屋配置类
import 'package:flutter/material.dart';
import 'package:hkzf_flutter/widgets/common_check_button.dart';
import '../config.dart';

class RoomApplianceItem {
   final String? title;// 标题
   final int? iconPoint;// 图标
   final bool? isChecked;// 是否选中

  const RoomApplianceItem(this.title, this.iconPoint, this.isChecked);
   
}

// 房屋配置数据类
List<RoomApplianceItem> _dataList = [
  const RoomApplianceItem('衣柜', 0xe918, false),
  const RoomApplianceItem('洗衣机', 0xe917, false),
  const RoomApplianceItem('空调', 0xe90d, false),
  const RoomApplianceItem('天然气', 0xe90f, false),
  const RoomApplianceItem('冰箱', 0xe907, false),
  const RoomApplianceItem('暖气', 0xe910, false),
  const RoomApplianceItem('电视', 0xe908, false),
  const RoomApplianceItem('热水器', 0xe912, false),
  const RoomApplianceItem('宽带', 0xe90e, false),
  const RoomApplianceItem('沙发', 0xe913, false),
];


// 房源配置(可选）组件
class RoomApplianceItemWidget extends StatefulWidget {
  final ValueChanged<List<RoomApplianceItem>>? onChange;
  const RoomApplianceItemWidget({super.key, this.onChange});

  @override
  State<RoomApplianceItemWidget> createState() => _RoomApplianceItemWidgetState();
}

class _RoomApplianceItemWidgetState extends State<RoomApplianceItemWidget> {
  List<RoomApplianceItem> list = [];

  @override
  void initState() {
    super.initState();
    list = _dataList;
  }
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Wrap(
        runSpacing: 20.0,
        children: list.map((item)=>GestureDetector(
          onTap: (){
            print('当前项:${item.title}');
            setState(() {
              // 注意flutter中的map的箭头后面是=> 表达式，返回值，所以这里不能用{}包裹，而是用()包裹，表示返回值，而不是代码块，代码块用{}包裹，表示代码块，而不是返回值
              list = list.map((pre)=>(
                pre.title  == item.title  ? RoomApplianceItem(pre.title, pre.iconPoint, !(pre.isChecked ?? false)) : pre
              ),).toList();
            });
            // 过滤出选中项
            if(widget.onChange != null) widget.onChange!((list.where(((pre)=>pre.isChecked == true)).toList()));
          },
          child: SizedBox(
            width: MediaQuery.of(context).size.width / 5,
            child: Column(
              children: [
                 Icon(
                   IconData(item.iconPoint!,fontFamily: Config.CommonIcon),
                   size: 40.0,
                 ),
                 Padding(
                   padding: const EdgeInsets.all(10.0),
                   child: Text(item.title!),
                 ),
                 CommonCheckButton(isChecked: item.isChecked,)
              ],
            ),)
        )).toList(),
      ),
    );
  }
}

// 房源设施展示组件
class RoomApplianceList extends StatelessWidget {
  final List<String>? list;
  const RoomApplianceList({super.key, this.list});

  @override
  Widget build(BuildContext context) {
    // 过滤出展示列表数据
    var showList = _dataList.where((item)=>list!.contains(item.title)).toList();
    if(showList.length == 0){
      return Container(
         padding: const EdgeInsets.only(left: 10.0),
         child: const Text('暂无房屋配置信息'),
      );
    }
    return SizedBox(
      child: Wrap(
        runSpacing: 20.0,
        children: showList.map((item)=>SizedBox(
          width: MediaQuery.of(context).size.width / 5,
          child: Column(
            children: [
               Icon(
                 IconData(item.iconPoint!,fontFamily: Config.CommonIcon),
                 size: 40.0,
               ),
               Padding(
                 padding: const EdgeInsets.all(10.0),
                 child: Text(item.title!),
               ),
            ],
          ),)).toList(),
      ),
    );
  }
}